//
//  AKCommon.m
//  CocoaBean
//
//  Created by sleeper on 3/29/10.
//  Copyright 2010 __MyCompany__. All rights reserved.
//

#import "AKCommon.h"

//#error First replace the API key and secret with your own, then delete this line.
NSString * const kAKAPIKey = @"05ec1489228054b111c43d9b6780fe13";
NSString * const kAKSecret = @"37bb6c18e6dbfbb0";

NSString * const kAKAppName = @"com.douban.com-iPhone";
NSString * const kAKCallbackName = @"akcallback";

NSString * const kAKUserIdKeychainKey = @"com.douban.com-doubanId";

UINavigationController *AKNavigationControllerWithRootViewController(UIViewController *viewController)
{
	return [[[UINavigationController alloc] initWithRootViewController:viewController] autorelease];
}

NSString *AKStringForArrayOfStrings(NSArray *strings, NSString *separator)
{
	NSString *retVal = @"";
	NSMutableString *tempString = nil;
	
	if ([strings count] > 0) {
		tempString = [[NSMutableString alloc] init];
		BOOL shouldAppendSeparator = NO;
		for (NSString *string in strings) {
			if (shouldAppendSeparator && separator != nil)
				[tempString appendString:separator];
			[tempString appendString:string];
			shouldAppendSeparator = YES;
		}
		
		retVal = [[tempString copy] autorelease];
		[tempString release];
	}
	
	return retVal;
}

static const void * AKRetainNoOp(CFAllocatorRef allocator, const void *value) { return value; }
static void AKReleaseNoOp(CFAllocatorRef allocator, const void *value) { }

NSMutableArray * AKCreateNonRetainingArray()
{
	CFArrayCallBacks callbacks = kCFTypeArrayCallBacks;
	callbacks.retain = AKRetainNoOp;
	callbacks.release = AKReleaseNoOp;
	return (NSMutableArray*)CFArrayCreateMutable(nil, 0, &callbacks);
}

NSMutableSet * AKCreateNonRetainingSet()
{
	CFSetCallBacks callbacks = kCFTypeSetCallBacks;
	callbacks.retain = AKRetainNoOp;
	callbacks.release = AKReleaseNoOp;
	return (NSMutableSet*)CFSetCreateMutable(nil, 0, &callbacks);
}

NSMutableDictionary * AKCreateNonRetainingDictionary()
{
	CFDictionaryKeyCallBacks keyCallbacks = kCFTypeDictionaryKeyCallBacks;
	CFDictionaryValueCallBacks callbacks = kCFTypeDictionaryValueCallBacks;
	callbacks.retain = AKRetainNoOp;
	callbacks.release = AKReleaseNoOp;
	return (NSMutableDictionary*)CFDictionaryCreateMutable(nil, 0, &keyCallbacks, &callbacks);
}

NSString * AKCreateUUID()
{
	CFUUIDRef uid = CFUUIDCreate(kCFAllocatorDefault);
	NSString *rc = (NSString *)CFUUIDCreateString(kCFAllocatorDefault, uid);
	CFRelease(uid);
	return rc;
}